package com.qfedu.dao.impl;

import com.qfedu.dao.ProductDao;
import com.qfedu.dao.UserDao;
import com.qfedu.pojo.MyProduct;
import com.qfedu.pojo.Product;

import java.math.BigDecimal;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ProductDaoImpl extends BaseDao implements ProductDao {
    @Override
    public List<MyProduct> findMyProduct(String idcode) {
        String sql = "SELECT * FROM \n" +
                "(SELECT a.id,a.pid,a.idcode,a.pnum,a.payDate,a.backDate,a.paypassword,a.money,p.pname,p.prate,p.limitDate,p.price,p.trend,p.beginPrice,p.upPrice\n" +
                "FROM (SELECT m.id,m.pid,m.idcode,m.pnum,m.payDate,m.backDate,a.paypassword,a.money\n" +
                "FROM bank_myproduct AS m\n" +
                "LEFT JOIN bank_account AS a ON m.idcode = a.idcode) AS a\n" +
                "LEFT JOIN bank_products AS p ON a.pid = p.pid)q WHERE q.idcode = ?";
       return queryForList(MyProduct.class,sql,idcode);
    }

    @Override
    public List<Product> findAllProducts() {
        String sql = "select * from bank_products";
        return queryForList(Product.class,sql);
    }

    @Override
    public Product findProductById(int id) {
        String sql = "select * from bank_products where pid = ?";
        return queryForOne(Product.class,sql,id);
    }

    @Override
    public void addMyProductById(Connection connection, String idcode, int pid, BigDecimal pnum) {
        String sql = "insert into bank_myproduct values(?,?,?,?,?,?)";
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        update(connection,sql,null,pid,idcode,pnum,format,null);
    }

    @Override
    public MyProduct findMyProductByOnly(int id) {
        String sql = "SELECT * FROM \n" +
                "(SELECT a.id,a.pid,a.idcode,a.pnum,a.payDate,a.backDate,a.paypassword,a.money,p.pname,p.prate,p.limitDate,p.price,p.trend,p.beginPrice,p.upPrice\n" +
                "FROM (SELECT m.id,m.pid,m.idcode,m.pnum,m.payDate,m.backDate,a.paypassword,a.money\n" +
                "FROM bank_myproduct AS m\n" +
                "LEFT JOIN bank_account AS a ON m.idcode = a.idcode) AS a\n" +
                "LEFT JOIN bank_products AS p ON a.pid = p.pid)q WHERE q.id = ?";
        return queryForOne(MyProduct.class,sql,id);
    }

    @Override
    public int deleteMyProductById(Connection connection, Integer id) {
        String sql = "delete from bank_myproduct where id = ?";
        return update(sql,id);
    }

    @Override
    public int updateMyProductById(Connection connection, Integer id, int leftNum) {
        String sql = "update bank_myproduct set pnum = ? where id = ?";
        return update(sql,leftNum,id);
    }

    @Override
    public BigDecimal backAllProductsPrice(String idcode) {
        List<MyProduct> myProduct = findMyProduct(idcode);
        BigDecimal total = new BigDecimal(0);
        for (MyProduct my:myProduct) {
            Integer pnum = my.getPnum();
            //获取当前商品单价
            BigDecimal price = my.getPrice();
            //获取收益率
            Double prate = my.getPrate();
            //获取购买日期
            Date payDate = my.getPayDate();
            long payDateTime = payDate.getTime();
            //获取当前赎回日期
            Date date = new Date();
            long backDateTime = date.getTime();
            //获取期限
            int m = my.getLimitDate();
            long limitDateTime = (long)m * 24 * 60 * 60 * 1000;
            //单价*份数*(1+收益率*买入日期/期限)-单价*1/10
            //份数*(1+收益率*买入日期/期限)
            BigDecimal bigDecimal1 = new BigDecimal(pnum*(1.00+prate*(backDateTime-payDateTime)/limitDateTime));
            //单价*份数*(1+收益率*买入日期/期限)-单价*1/10
            BigDecimal mon = price.multiply(bigDecimal1.subtract(new BigDecimal(0.1)));
            total = total.add(mon);
        }
        return total;
    }
}
